extends GPUParticles2D

const PARTICLE_HEAL_HP_EFFECT_BALL = preload("uid://h06vvtqhn7b8")

@export var speed: float = 300.0
@export var scatter_friction: float = 1000
@export var initial_scatter_time: float = 0.3

var target: UnitBase
var velocity: Vector2
var scatter_timer: float = 0.0
var heal_point: float = 0
var arrive_target: bool = false


func _ready():
	# 初始随机速度
	velocity = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized() * speed
	scatter_timer = initial_scatter_time


func _process(delta: float):
	if arrive_target: return
	
	if scatter_timer > 0:
		scatter_timer -= delta
		global_position += velocity * delta
		velocity = velocity.move_toward(Vector2.ZERO, scatter_friction * delta)
	else:
		var dir = (target.ray_cast_point.global_position - global_position).normalized()
		var target_velocity = dir * speed
		velocity = velocity.lerp(target_velocity, 0.05)
		global_position += velocity * delta
	
		if global_position.distance_to(target.ray_cast_point.global_position) < 3.0:
			arrive_target = true
			emitting = false
			
			var target_unit = target as UnitBase
			target_unit.heal(heal_point)
			
			await get_tree().create_timer(1.0).timeout
			queue_free()
			
			
			
			
			
